#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include "RingQueue.hpp"
#include "Task.hpp"
 
using namespace std;
// 数据
struct ThreadData
{
    string threadname;
    Task<int> task;
};
// 消费者
void *Customer(void *args)
{
    RingQueue<ThreadData*>* rq = static_cast<RingQueue<ThreadData*>*>(args);
    for (int i = 0; i < 4; i++)
    {
        rq->pop();
        sleep(1);
    }
    return nullptr;
}
// 生产者
void *Producer(void *args)
{
    RingQueue<ThreadData*>* rq = static_cast<RingQueue<ThreadData*>*>(args);
    for (int i = 0; i < 4; i++)
    {
        int len = opers.size();
        int data1 = rand() % 10 + 1;
        int data2 = rand() % 10;
        char op = opers[rand() % len];
        Task<int> t(data1, data2, op);
        ThreadData* data = new ThreadData;
        data->threadname = "Producer: data->" + to_string(i + 1);
        data->task = t;  
        rq->push(data);
    }
    return nullptr;
}
int main()
{
    srand(time(nullptr) ^ getpid());
    pthread_t c[5], p[5];
    RingQueue<ThreadData*>* rq = new RingQueue<ThreadData*>;
    // 生产者线程
    for (int i = 0; i < 3; i++)
    {
        pthread_create(p + i, nullptr, Producer, rq);
        sleep(1);
    }
    // 消费者线程
    for (int i = 0; i < 3; i++)
    {
        pthread_create(c + i, nullptr, Customer, rq);
        sleep(1);
    }
    for (int i = 0; i < 3; i++)
        pthread_join(c[i], nullptr);
    for (int i = 0; i < 3; i++)
        pthread_join(p[i], nullptr);
    delete rq;
    return 0;
}